package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.LinkReserverAdapter;
import com.sxm.yiwei.adapters.SListViewSlideAdapter;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MineServicer extends BaseActivity implements View.OnClickListener{

    private PullToRefreshListView reserverlist;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private JSONArray array;

    private List<HashMap<String,String>>linkList;
    private HashMap<String,String>map;
    private Intent in;
    private ImageView back;
    private String is = "0";
    private LinkReserverAdapter adapter;
    int page = 1;
    private static final int ADDRESSONE = 1003;
    private static final int ADDRESSTWO = 1002;
    private static final int ADDRESSTHREE = 1001;
    private TextView addlink;

    @Override
    protected void onRestart() {
        super.onRestart();
       // getsLinkdata(page);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_servicer);
        sharedPreferences =getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        reserverlist = (PullToRefreshListView) findViewById(R.id.mine_servicer_list);
        addlink = (TextView) findViewById(R.id.mine_reserveradd);
        back = (ImageView) findViewById(R.id.minereservcer_back);
        back.setOnClickListener(this);
        addlink.setOnClickListener(this);
        linkList = new ArrayList<>();
        getsLinkdata(page);

    }


    // 获取联系人数据
    private void getsLinkdata(int page) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/contact/getContactList");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("type","0");
        j.put("pageNo",page+"");
        j.put("pageSize","20");
        params.addParameter("params",j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                getData(result);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void getData(String result) {

        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        String data = obj.getString("data");
        array = JSONArray.parseArray(data);
        for (int i = 0; i < array.size(); i++) {
            map = new HashMap();
            com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(String.valueOf(array.get(i)));
            String name = obj1.getString("name");
            String longitude = obj1.getString("longitude");
            String latitude = obj1.getString("latitude");
            String phone = obj1.getString("phone");
            String address = obj1.getString("address");
            String id = obj1.getString("id");
            map.put("name",name);
            map.put("phone",phone);
            map.put("address",address);
            map.put("id",id);
            map.put("longitude",longitude);
            map.put("latitude",latitude);
            linkList.add(map);
            // 设置listview
        }
        adapter = new LinkReserverAdapter(MineServicer.this,linkList);
        reserverlist.setAdapter(adapter);
        reserverlist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getsLinkdata(page);
                new FinishRefresh().execute();
            }
        });
        reserverlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, String>item = (HashMap) adapter.linkList.get(position-1);
                String address1 = item.get("address");
                String phone1 = item.get("phone");
                String name1 = item.get("name");
                String id1 = item.get("id");
                String latitude = item.get("latitude");
                String longitude = item.get("longitude");
                Bundle bundle = new Bundle();
                bundle.putString("address1",address1);
                bundle.putString("phone1",phone1);
                bundle.putString("latitude",latitude);
                bundle.putString("longitude",longitude);
                bundle.putString("name1",name1);
                bundle.putString("id", String.valueOf(id1));
                if (ADDRESSONE==1003){
                    in = new Intent(MineServicer.this, FwBuyServiceActivity.class);
                    in.putExtras(bundle);
                }else if (ADDRESSTWO==1002){
                    in = new Intent(MineServicer.this, MineAdresser.class);
                    in.putExtras(bundle);
                }else if (ADDRESSTHREE==1001){
                    in = new Intent(MineServicer.this, OnceSendActivity.class);
                    in.putExtras(bundle);
                }else {}

                MineServicer.this.setResult(Activity.RESULT_OK, in);
                finish();

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_reserveradd:
                Intent intent = new Intent(MineServicer.this,AddLinkmanActivity.class);
                intent.putExtra("is",is);
                startActivity(intent);
                break;
            case R.id.minereservcer_back:
                finish();
                break;
        }
    }

    private class FinishRefresh extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            adapter.notifyDataSetChanged();
            reserverlist.onRefreshComplete();
        }
    }
}
